package com.yunshu.yunshuapp.push;

/**
 * Created by zhouwenjie on 2017/7/9.
 */

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.PushConsts;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.message.FeedbackCmdMessage;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.igexin.sdk.message.SetTagCmdMessage;
import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.yunshu.yunshuapp.kuaixun.KuaixundetailActivity;
import com.yunshu.yunshuapp.user.YSUser;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 继承 GTIntentService 接收来自个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则无法接受消息<br>
 * onReceiveMessageData 处理透传消息<br>
 * onReceiveClientId 接收 cid <br>
 * onReceiveOnlineState cid 离线上线通知 <br>
 * onReceiveCommandResult 各种事件处理回执 <br>
 */
public class GetuiIntentService extends GTIntentService {

    public GetuiIntentService() {

    }

    @Override
    public void onReceiveServicePid(Context context, int pid) {
    }

    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
        Log.e(TAG, "onReceiveClientId -> " + "clientid = " + msg);
        if (msg.getPayload() != null){
            try {
                String s = new String(msg.getPayload(),"utf-8");
                System.out.print(s);
                if(s.length() > 11){
                    String kuaixunId = s.substring(10,s.length()-3);
                    if (kuaixunId != null && kuaixunId.length() > 0){
                        Intent intent = new Intent();
                        intent.setClass(context.getApplicationContext(), KuaixundetailActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        Bundle bundle = new Bundle();
                        bundle.putString("kxid",kuaixunId);
                        intent.putExtras(bundle);
                        context.getApplicationContext().startActivity(intent);
                    }
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return;
            }
        }

    }

    @Override
    public void onReceiveClientId(Context context, String clientid) {
        Log.e(TAG, "onReceiveClientId -> " + "clientid = " + clientid);

        if (clientid != null && clientid.length() > 0) {
            YSUser.setGetuiPushChannel(clientid);

        }
    }

    @Override
    public void onReceiveOnlineState(Context context, boolean online) {
    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage cmdMessage) {
    }
}